import { result } from 'lodash';
/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-08-11 11:31:57
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-12-06 18:04:21
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
import { CreateModifManageInput, MoadifManageServiceProxy, CommonServiceProxy } from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { MaintainComponent } from '../maintain/maintain.component';
import { ProdureApprovalComponent } from '../produre-approval/produre-approval.component';
import { WatchMaintainComponent } from '../watch-maintain/watch-maintain.component';

@Component({
    selector: 'addOrEddit',
    templateUrl: './add-or-eddit.component.html',
    styleUrls: ['./add-or-eddit.component.less']
})
export class AddOrEdditComponent extends AppComponentBase {
    @ViewChild('ProdureApprovalComponent') ProdureApprovalComponent: ProdureApprovalComponent;
    @ViewChild('WatchMaintainComponent') WatchMaintainComponent: WatchMaintainComponent;
    @ViewChild('maintain') maintain: MaintainComponent;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() editData: any;  //行数据
    @Input() title: string;  //标题
    saving = false;
    active = false;
    form = new CreateModifManageInput();
    selectRow: any;
    sonTitle = undefined;
    sendTimeOptions = [];
    moduleOptions = [];
    changeTypeOption = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    reasonOption = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    changeId = undefined;
    maintTitle = undefined;
    saveFlag = true;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _moadifManageServiceProxy: MoadifManageServiceProxy,
        private _commonServiceProxy: CommonServiceProxy,
    ) {
        super(injector);
    }

    ngOnInit() {
    }
    //打开弹窗
    onShown(): void {
        let guid = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
        let guidCode = guid + guid + "-" + guid + "-" + guid + "-" + guid + "-" + guid + guid + guid;
        this.changeId = guidCode;
        this.active = true;
        this.changeTypeOption = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._commonServiceProxy.getTypeList(
            "变更类型"
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.changeTypeOption = result;
            });
        this.reasonOption = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._commonServiceProxy.getTypeList(
            "变更原因"
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.reasonOption = result;
            });
        //型号
        this._commonServiceProxy.getModelList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.moduleOptions = [];
                this.moduleOptions = result;
            });
    }
    onChange(event) {
        //发次
        this._commonServiceProxy.getLotNoList(
            this.form.modelId
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.sendTimeOptions = [];
                this.sendTimeOptions = result;
            });
    }
    //查看新增任务
    show(): void {
        if (this.title == '变更修改' || this.title == '规程变更查看') {
            this.initEditInfo();//初始化数据
        }
        this.active = true;
        this.modal.show();
    }
    initEditInfo() {
        this.form.id = this.editData.id;
        this.form.destinationTechniqueId = this.editData.destinationTechniqueId;
        this.form.drawingNo = this.editData.drawingNo;
        this.form.content = this.editData.content;
        this._commonServiceProxy.getLotNoList(
            this.form.modelId
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.sendTimeOptions = [];
                this.sendTimeOptions = result;
                this.form.lotNo = this.editData.lotNo;
                this.form.lotNoId = this.editData.lotNoId;
            });
        this.form.model = this.editData.model;
        this.form.modelId = this.editData.modelId;
        this.form.modelName = this.editData.modelName;
        this.form.modifyNumber = this.editData.modifyNumber;
        this.form.modifyReason = this.editData.modifyReason;
        this.form.modifyType = this.editData.modifyType;
        this.form.productOpinions = this.editData.productOpinions;
        this.form.reEdition = this.editData.reEdition;
        this.form.remark = this.editData.remark;
        this.form.stageSign = this.editData.stageSign;
        this.form.techniqueId = this.editData.techniqueId;
        this.form.techniqueName = this.editData.techniqueName;
        this.form.techniqueNumber = this.editData.techniqueNumber;
        this.form.createFlag = 1;
        this.primengTableHelper.records = [{
            id: this.form.techniqueId,
            techniqueNumber: this.form.techniqueNumber,
            techniqueName: this.form.techniqueName,
            edition: this.form.reEdition,
            moduleCode: this.form.moduleCode,
            modelId: this.form.modelId,
            lotNoId: this.editData.lotNoId
        }]
        this.primengTableHelper.totalRecordsCount = 1;
        this.selectRow = {
            id: this.form.techniqueId,
            techniqueNumber: this.form.techniqueNumber,
            techniqueName: this.form.techniqueName,
            edition: this.form.reEdition,
            moduleCode: this.form.moduleCode,
            destinationTechniqueId: this.editData.destinationTechniqueId,
            modelId: this.form.modelId,
            lotNoId: this.editData.lotNoId
        }
    }
    //关闭事件
    close(): void {
        //清空form表单
        this.primengTableHelper.records = [];
        this.form = new CreateModifManageInput();
        this.selectRow = [];
        this.active = false;
        this.modal.hide();
    }
    // 获取表格
    getTableList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this._moadifManageServiceProxy.getMoadifTechnique(
            this.form.modelId,
            this.form.lotNoId,
            undefined,
            undefined,
            undefined,
            undefined,
            this.form.techniqueName,
            this.form.techniqueNumber,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }
    resert() {
        this.form.techniqueNumber = undefined;
        this.form.techniqueName = undefined;
    }
    toMaintain() {
        if (this.selectRow == undefined || this.selectRow == null) {
            this._messageService.error('请先选择规程再进行维护');
            return;
        }
        if (this.title == '规程变更查看') {
            this.WatchMaintainComponent.show();
            this.WatchMaintainComponent.getTreeInfo();
        } else {
            this.sonTitle = '工艺规程管理';
            this.maintain.show();
        }

    }
    backApproval(row) {
        this.modalSave.emit();
        this.close();
    }
    backMaintain(row) {
    }
    save() {
        if (this.title == "变更申请") {
            this.form.techniqueNumber = this.selectRow.techniqueNumber;
            this.form.techniqueName = this.selectRow.techniqueName;
            this.form.reEdition = this.selectRow.edition;
            this.form.drawingNo = this.selectRow.moduleCode;
            this.form.techniqueId = this.selectRow.id;
            this.form.model = this.selectRow.model;
            this.form.modelId = this.selectRow.modelId;
            this._commonServiceProxy.getLotNoList(
                this.form.modelId
            ).pipe(finalize(() => { }))
                .subscribe(result => {
                    this.sendTimeOptions = [];
                    this.sendTimeOptions = result;
                    this.form.lotNo = this.selectRow.lotNo;
                    this.form.lotNoId = this.selectRow.lotNoId;
                });
            this.form.modelName = this.selectRow.modelName;
            this.form.stageSign = this.selectRow.stageSign;
            this.form.createFlag = 0;
        } else {
            if (this.editData.documentStatus != 0 && this.editData.documentStatus != 2) {
                this._messageService.info('该数据状态为：' + this.editData.documentStatusDesc);
                return;
            }
        }
        if (this.saveFlag) {
            this.saveFlag = false;
            this._moadifManageServiceProxy.holdMoadifManage(this.form)
                .pipe(finalize(() => { }))
                .subscribe((result) => {
                    this.saveFlag = true;
                    if (result.code == 0) {
                        this._messageService.success('保存成功');
                        this.modalSave.emit();
                        this.close();
                    } else {
                        this._messageService.error(result.message)
                    }
                });
        }
    }
    saveToapproval() {
        if (this.title == "变更申请") {
            this.form.techniqueNumber = this.selectRow.techniqueNumber;
            this.form.techniqueName = this.selectRow.techniqueName;
            this.form.reEdition = this.selectRow.edition;
            this.form.drawingNo = this.selectRow.moduleCode;
            this.form.model = this.selectRow.model;
            this.form.techniqueId = this.selectRow.id;
            this.form.modelId = this.selectRow.modelId;
            this.form.lotNo = this.selectRow.lotNo;
            this.form.lotNoId = this.selectRow.lotNoId;
            this.form.modelName = this.selectRow.modelName;
            this.form.stageSign = this.selectRow.stageSign;
            this.form.createFlag = 0;
        } else {
            if (this.editData.documentStatus != 0 && this.editData.documentStatus != 2) {
                this._messageService.info('该数据状态为：' + this.editData.documentStatusDesc);
                return;
            }
        }
        this.maintTitle = '审批维护';
        setTimeout(() => {
            this.ProdureApprovalComponent.show();
        }, 10);
    }
}
